﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace AUV5.Web.Handler
{
    /// <summary>
    /// CutAvatarHandler 的摘要说明
    /// </summary>
    public class CutAvatarHandler : IHttpHandler,IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Charset = "utf-8";

            bool isLogin = new PageBaseTop().IsVerified;    //是否登陆

            System.Drawing.Bitmap bitmap = null;   //按截图区域生成Bitmap
            System.Drawing.Image thumbImg = null;      //被截图 
            System.Drawing.Graphics gps = null;    //存绘图对象   
            System.Drawing.Image finalImg = null;  //最终图片

            if (isLogin)
            {
                try
                {
                    string pointX = context.Request.Params["pointX"];   //X坐标
                    string pointY = context.Request.Params["pointY"];   //Y坐标
                    string imgUrl = context.Request.Params["imgUrl"];   //被截图图片地址
                    string rlSize = context.Request.Params["maxVal"];        //截图矩形的大小
                    
                    if (!string.IsNullOrEmpty(pointX) && !string.IsNullOrEmpty(pointY) && !string.IsNullOrEmpty(imgUrl))
                    {
                        string ext = System.IO.Path.GetExtension(imgUrl).ToLower();   //上传文件的后缀（小写）
                        
                        bitmap = new System.Drawing.Bitmap(Convert.ToInt32(rlSize), Convert.ToInt32(rlSize));

                        thumbImg = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(imgUrl));
                        
                        System.Drawing.Rectangle rl = new System.Drawing.Rectangle(Convert.ToInt32(pointX), Convert.ToInt32(pointY), Convert.ToInt32(rlSize), Convert.ToInt32(rlSize));   //得到截图矩形

                        gps = System.Drawing.Graphics.FromImage(bitmap);      //读到绘图对象

                        gps.DrawImage(thumbImg, 0, 0, rl, System.Drawing.GraphicsUnit.Pixel);
                        
                        finalImg = AUV5.Common.FileHelper.GetThumbNailImage(bitmap, AUV5.Common.CommonDefine.avatarMinWidth,AUV5.Common.CommonDefine.avatarMinHeight);

                        string finalPath = new PageBaseTop().GetDirectoryOfDate(false);

                        if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath(finalPath)))   //不存在上传目录则创建
                        {
                            System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(finalPath));
                        }

                        string userId = new PageBaseTop().VerifiedUserID;

                        string relaPath = finalPath + userId + ext;
                        string absPath = HttpContext.Current.Server.MapPath(finalPath) + userId + ext;

                        finalImg.Save(absPath);

                        bitmap.Dispose();
                        thumbImg.Dispose();
                        gps.Dispose();
                        finalImg.Dispose();
                        GC.Collect();
                        
                        //AUV5.Common.FileHelper.DeleteFile(HttpContext.Current.Server.MapPath(imgUrl));

                        AUV5.Model.UserInfo userInfo = AUV5.BLL.UserInfoBLL.Instance.GetDetail(userId);
                        userInfo.PhotoPath = relaPath;
                        
                        AUV5.BLL.UserInfoBLL.Instance.Update(userInfo);

                        new PageBaseTop().VerifiedInfo = new AUV5.Model.VerifiedInfo(userInfo);

                        context.Response.Write(relaPath);
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
            else
            {
                context.Response.Write("erorr");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}